package com.mxpio.erp.mes.controller;

import com.mxpio.erp.common.mes.entity.ManufactureWorkOrderTask;
import com.mxpio.erp.common.mes.service.ManufactureWorkOrderTaskService;
import com.mxpioframework.common.vo.Result;
import com.mxpioframework.jpa.query.Criteria;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.web.bind.annotation.*;

import java.io.UnsupportedEncodingException;
import java.util.List;

@Tag(name = "ManufactureWorkOrderTaskController", description = "生产订单-工单-工序任务接口")
@RestController
@RequestMapping("/erp/mes/mowo/task/")
public class ManufactureWorkOrderTaskController {
	@Autowired
	private ManufactureWorkOrderTaskService service;

	@GetMapping("list")
	@Operation(summary = "生产订单-工单-工序任务列表", description = "获取生产订单-工单-工序任务列表", method = "GET")
	public Result<List<ManufactureWorkOrderTask>> list(Criteria criteria) throws UnsupportedEncodingException {
		List<ManufactureWorkOrderTask> items = service.list(ManufactureWorkOrderTask.class, criteria);
		return Result.OK(items);
	}
	
	@GetMapping("page")
	@Operation(summary = "生产订单-工单-工序任务列表", description = "获取生产订单-工单-工序任务列表", method = "GET")
	public Result<Page<ManufactureWorkOrderTask>> page(Criteria criteria, Integer pageSize, Integer pageNo) throws UnsupportedEncodingException {
		Pageable page = PageRequest.of(pageNo-1, pageSize);
		Page<ManufactureWorkOrderTask> items = service.listPage(ManufactureWorkOrderTask.class, page, criteria);
		return Result.OK(items);
	}
	
	@GetMapping("list/{taskCode}")
	@Operation(summary = "根据taskCode获取生产订单-工单-工序任务", description = "根据taskCode获取生产订单-工单-工序任务", method = "GET")
	public Result<ManufactureWorkOrderTask> getById(@PathVariable(name = "taskCode", required = true) String taskCode) {
		ManufactureWorkOrderTask item = service.getById(ManufactureWorkOrderTask.class, taskCode);
		return Result.OK(item);
	}

	@Operation(summary = "工序任务-派工（工序任务分配到具体作业单元）", description = "工序任务-派工（工序任务分配到具体作业单元）", method = "PUT")
	@PutMapping(value = "dispatch")
	public Result<?> send(@RequestBody ManufactureWorkOrderTask task) {
		return service.dispatch(task);
	}

	@RequestMapping("start/{taskCode}")
	@Operation(summary = "生产订单-工单-工序任务-开工", description = "生产订单-工单-工序任务-开工")
	public Result<?> release(@PathVariable(name = "taskCode", required = true) String taskCode) {
		return service.start(taskCode);
	}
/*	@DeleteMapping("remove/{taskCodes}")
	@Operation(summary = "根据taskCode删除", description = "删除", method = "DELETE")
	public Result<ManufactureWorkOrderTask> remove(@Parameter(description="产碰编码，多个单号用英文逗号分隔") @PathVariable(name = "taskCodes", required = true) String taskCodes) {
		String[] codeArray = taskCodes.split(",");
		for(String key : codeArray){
			service.delete(ManufactureWorkOrderTask.class, key);
		}
		return Result.OK();
	}*/

}
